import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class Client {
    private DatagramSocket socket = null;
    //服务器ip地址
    private String severIp;
    //服务器的端口号
    private int serverport;


    public Client(String severIp, int serverport) throws SocketException {
        this.severIp = severIp;
        this.serverport = serverport;
        socket = new DatagramSocket();
    }

    public void start() throws IOException {
        Scanner scanner =new Scanner(System.in);
        while (true){
            System.out.println("输入要发送的内容");
            if(!scanner.hasNext()){
                break;
            }
            String request = scanner.next();

            DatagramPacket datagramPacket = new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(severIp),serverport);

            socket.send(datagramPacket);

            DatagramPacket responsePage = new DatagramPacket(new byte[4090],4090);

            socket.receive(responsePage);
            //得到从服务器处理完的数据,将其转换成字符串类型,然后输出
            String response = new String(responsePage.getData(),0, responsePage.getLength());
            System.out.println(response);
        }
    }

    public static void main(String[] args) throws IOException {
        Client client = new Client("127.0.0.1",9090);
        client.start();
    }
}
